﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Leap;

namespace LeapDemo002
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		Controller _controller = new Controller();
		DrawingAttributes _drawingAttributes = new DrawingAttributes();

		public MainWindow()
		{
			InitializeComponent();

			_drawingAttributes.Width = 20;
			_drawingAttributes.Height = 20;
			_drawingAttributes.StylusTip = StylusTip.Ellipse;

			CompositionTarget.Rendering += CompositionTarget_Rendering;
		}

		void CompositionTarget_Rendering(object sender, EventArgs e)
		{
			Presenter.Strokes.Clear();
			
			var width = Width;
			var height = Height;

			var frame = _controller.Frame();
			var interactionBox = frame.InteractionBox;

			foreach (var pointable in frame.Pointables)
			{
				var pos = pointable.StabilizedTipPosition;
				var npos = interactionBox.NormalizePoint(pos);

				var x = width*npos.x;
				var y = height*(1 - npos.y);

				if (pointable.TouchDistance > 0)
					_drawingAttributes.Color = Colors.Green;
				else
				{
					_drawingAttributes.Color = Colors.Red;
				}

				var point = new StylusPoint(x,y);
				var points = new StylusPointCollection() {point};
				var stroke = new Stroke(points, _drawingAttributes);

				Presenter.Strokes.Add(stroke);
			}

		}
	}
}
